<?php
App::uses('AppController', 'Controller');
/**
 * ArticleCates Controller
 *
 * @property ArticleCate $ArticleCate
 * @property PaginatorComponent $Paginator
 */
class ArticleCatesController extends AppController {

/**
	 * 资讯分类列表
	 * @author mingxing.zhao@qq.com
	 * @Date 2013-12-29
	 * @param
	 * @return
	 */
	public function admin_index() {
		$url = array('admin'=>true,'controller'=>'ArticleCates','action'=>'jsonPage');
		$param = $this->ArticleCate->pageParam($url);
		$this->set('paginationScript',$this->ArticleCate->showTreePagination($param));
	}
	
	/**
	 *
	 * 用户查询数据
	 * @author mingxing.zhao@qq.com
	 * @Date 2013-12-29
	 * @param
	 * @return
	 */
	function admin_jsonPage(){
		$this->autoRender = false;
	
		$return = array(
			'page'=>0,			 
			'total'=>0,			 
			'records'=>0,		 
			'rows'=>array()		 
		);
		$parentId = empty($_POST['nodeid'])?1:$_POST['nodeid'];
		$level = empty($_POST['n_level'])?0:$_POST['n_level'];
		$rows = $this->ArticleCate->ajaxTreeCategory($parentId,$level+1);
		$return['rows'] =$rows;
		
		echo json_encode($return);
	}
	

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add($parent_id = 1) {
		if ($this->request->is('post')) {
			$this->ArticleCate->create();
			if ($this->ArticleCate->save($this->request->data)) {
				$this->Session->setFlash(__('The article cate has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The article cate could not be saved. Please, try again.'));
			}
		}
		$this->set('parent_id',$parent_id);
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->ArticleCate->exists($id)) {
			throw new NotFoundException(__('Invalid article cate'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->ArticleCate->save($this->request->data)) {
				$this->Session->setFlash(__('The article cate has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The article cate could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('ArticleCate.' . $this->ArticleCate->primaryKey => $id));
			$this->request->data = $this->ArticleCate->find('first', $options);
		}
	}

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->ArticleCate->id = $id;
		if (!$this->ArticleCate->exists()) {
			throw new NotFoundException(__('Invalid article cate'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->ArticleCate->delete()) {
			$this->Session->setFlash(__('The article cate has been deleted.'));
		} else {
			$this->Session->setFlash(__('The article cate could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}}
